package com.bw.wxy.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bw.wxy.R;
import com.bw.wxy.action.BroadAction;
import com.bw.wxy.common.SpUtils;
import com.bw.wxy.mvp.model.entity.BaseEntity;
import com.bw.wxy.mvp.model.entity.ResponseUserEntity;
import com.bw.wxy.factroy.HttpCode;
import com.bw.wxy.mvp.base.view.BaseFragment;
import com.bw.wxy.mvp.contract.LoginContract;
import com.bw.wxy.mvp.di.DaggerLoginComponent;
import com.bw.wxy.mvp.di.LoginModules;
import com.bw.wxy.mvp.presenter.LoginPresenter;


public class Fragment_register extends BaseFragment<LoginPresenter> implements LoginContract.ILoginView {
    private EditText usernameEdit;
    private EditText pwdEdit;
    private Button registerBut;

//    @Override
//    public int addBarColor() {
//        return Color.WHITE;
//    }
//
//    @Override
//    public View addStatusView() {
//        return f(R.id.register_linear);
//    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_register;
    }

    @Override
    public void initView() {
        usernameEdit = f(R.id.username_edit);
        pwdEdit = f(R.id.pwd_edit);
        registerBut = f(R.id.register_but);

        registerBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = usernameEdit.getText().toString().trim();
                String pwd = pwdEdit.getText().toString().trim();
                if (username.isEmpty()||pwd.isEmpty()){
                    Toast.makeText(getActivity(), "用户名密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                p.getLogin(username,pwd, HttpCode.REGISTERCODE);
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void inject() {
        DaggerLoginComponent.builder().loginModules(new LoginModules(this)).build().register(this);
    }

    @Override
    public void showView(BaseEntity baseEntity) {
        if (baseEntity.status.equals("200")){
            SpUtils.editLogin();
        }
        ResponseUserEntity responseUserEntity= (ResponseUserEntity) baseEntity;
        Intent intent = new Intent();
        intent.setAction(BroadAction.UPDATEUSERACTION);
        intent.putExtra("userValues",responseUserEntity.getValues());
        getActivity().sendBroadcast(intent);
        getActivity().finish();
    }

    @Override
    public void showHeadImg(BaseEntity baseEntity) {

    }
}